package com.liy.framework.common.websocket.server;

import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SupportServer {
    protected static Logger log = LoggerFactory.getLogger(SupportServer.class);
    protected static int onlineCount = 0;
    protected static CopyOnWriteArraySet<SupportServer> webSocketSet = new CopyOnWriteArraySet();
    protected Session session;
    protected String clientId;
    protected String groupType;

    public SupportServer() {
    }

    @OnOpen
    public void onOpen(@PathParam("groupType") String groupType, @PathParam("clientId") String clientId, Session session) {
        this.session = session;
        this.clientId = clientId;
        this.groupType = groupType;
        webSocketSet.add(this);
        addOnlineCount();
        log.info("有新连接加入！当前客户端数量为" + getOnlineCount());

        try {
            this.sendMessage("success");
        } catch (IOException var5) {
            log.error("websocket IO异常");
        }

    }

    @OnClose
    public void onClose(@PathParam("groupType") String groupType, @PathParam("clientId") String clientId) {
        webSocketSet.remove(this);
        subOnlineCount();
        log.info("有一连接关闭！当前在线人数为" + getOnlineCount());
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        try {
            if (message != null && "heartbeat".equals(message.toLowerCase())) {
                this.sendMessage("success");
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public void onError(@PathParam("groupType") String groupType, @PathParam("clientId") String clientId, Session session, Throwable error) {
        log.error(this.groupType + ":" + this.clientId + ":发生错误:" + error.getMessage(), error);
    }

    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    public static void sendToClient(String clientId, String message) {
        log.info("client:" + clientId + ", message: " + message);
        Iterator var2 = webSocketSet.iterator();

        while(var2.hasNext()) {
            SupportServer server = (SupportServer)var2.next();

            try {
                if (server.clientId.equals(clientId)) {
                    server.sendMessage(message);
                    break;
                }
            } catch (Exception var5) {
                ;
            }
        }

    }

    public static void sendToAll(String message) {
        log.info(message);
        Iterator var1 = webSocketSet.iterator();

        while(var1.hasNext()) {
            SupportServer server = (SupportServer)var1.next();

            try {
                server.sendMessage(message);
            } catch (Exception var4) {
                ;
            }
        }

    }

    public static void sendToGroup(String groupId, String message) {
        log.info(message);
        Iterator var2 = webSocketSet.iterator();

        while(var2.hasNext()) {
            SupportServer server = (SupportServer)var2.next();

            try {
                if (server.groupType.equals(groupId)) {
                    server.sendMessage(message);
                }
            } catch (Exception var5) {
                ;
            }
        }

    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        ++onlineCount;
    }

    public static synchronized void subOnlineCount() {
        --onlineCount;
    }
}
